import asyncio
import asyncpg
from app.core.config import settings

async def create_database():
    """创建数据库"""
    # 连接到默认的 postgres 数据库
    sys_conn = await asyncpg.connect(
        user=settings.POSTGRES_USER,
        password=settings.POSTGRES_PASSWORD,
        host=settings.POSTGRES_HOST,
        port=settings.POSTGRES_PORT,
        database='postgres'
    )
    
    # 检查数据库是否存在
    exists = await sys_conn.fetchval(
        "SELECT 1 FROM pg_database WHERE datname = $1",
        settings.POSTGRES_DB
    )
    
    if not exists:
        # 创建数据库
        await sys_conn.execute(
            f'CREATE DATABASE "{settings.POSTGRES_DB}"'
        )
        print(f"数据库 {settings.POSTGRES_DB} 创建成功")
    else:
        print(f"数据库 {settings.POSTGRES_DB} 已存在")
    
    await sys_conn.close()

if __name__ == "__main__":
    asyncio.run(create_database()) 